import {
  NPopconfirm,
  type PopconfirmProps,
  type PopconfirmInst,
} from "naive-ui";
import type { HTMLAttributes } from "vue";
import { getSlotsDom } from "../utils";
import type { BaseComponentType } from "../types";

export type PopconfirmFactoryConstructorType = {
  props?: PopconfirmProps;
  attrs?: HTMLAttributes;
  defaultSlot?: BaseComponentType;
  iconSlot?: BaseComponentType;
  actionSlot?: BaseComponentType;
};

export class PopconfirmFactory {
  private attrs: HTMLAttributes = {};
  private props: PopconfirmProps = {};
  private defaultSlot: BaseComponentType = null;
  private iconSlot: BaseComponentType = null;
  private actionSlot: BaseComponentType = null;
  private domRef: PopconfirmInst | null = null;

  constructor(param?: PopconfirmFactoryConstructorType) {
    if (param?.props) this.setProps(param.props);
    if (param?.attrs) this.setAttrs(param.attrs);
    if (param?.defaultSlot) this.setDefault(param.defaultSlot);
    if (param?.iconSlot) this.setIcon(param.iconSlot);
    if (param?.actionSlot) this.setAction(param.actionSlot);
  }

  setAttrs(attrs: HTMLAttributes) {
    this.attrs = attrs;
    return this;
  }

  setProps(props: PopconfirmProps) {
    this.props = props;
    return this;
  }

  setDefault(defaultSlot: BaseComponentType) {
    this.defaultSlot = defaultSlot;
    return this;
  }

  setIcon(iconSlot: BaseComponentType) {
    this.iconSlot = iconSlot;
    return this;
  }

  setAction(actionSlot: BaseComponentType) {
    this.actionSlot = actionSlot;
    return this;
  }

  setShow(show: boolean) {
    this.domRef?.setShow(show);
  }
  syncPosition() {
    this.domRef?.syncPosition();
  }

  create() {
    return (
      <NPopconfirm
        ref={(el: HTMLElement) => {
          this.domRef = el as unknown as PopconfirmInst;
        }}
        {...this.attrs}
        {...this.props}
      >
        {{
          default:
            this.defaultSlot != null
              ? () => getSlotsDom(this.defaultSlot)
              : null,

          icon: this.iconSlot != null ? () => getSlotsDom(this.iconSlot) : null,
          action:
            this.actionSlot != null ? () => getSlotsDom(this.actionSlot) : null,
        }}
      </NPopconfirm>
    );
  }
}
